package com.webmeeting.jsfbeans.chat.wordchat;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import com.myframework.utils.constants.SiteConstants;
import com.myframework.utils.logutil.WMLogger;
import com.myframework.utils.stringutil.StringUtil;
import com.webmeeting.chat.wordchat.bl.ChatRoom;
import com.webmeeting.chat.wordchat.dao.entities.User;
import com.webmeeting.chat.wordchat.servlets.WordChatInitServlet;

@ManagedBean(name = "roomLgoinBean_wordchat")
@RequestScoped
public class RoomLgoinBean {

	// Header message
	private String	headerMsg;
	// Access password
	private String	pwForAccess;
	// Delete password
	private String	pwForDelete;

	public String joinRoom(String contextPath) {
		FacesContext fc = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) fc.getExternalContext().getSession(true);

		String roomName = (String) session.getAttribute("roomName");
		List<ChatRoom> rooms = WordChatInitServlet.getOnlineRoomList();

		for (ChatRoom room : rooms) {
			if (roomName.equals(room.getRoomName())) {
				if (this.pwForAccess.equals(room.getPwForAccess())) {
					return contextPath + "/chat/wordchat/chatroom.xhtml?" + SiteConstants.FACES_REDIRECT;
				} else {
					this.headerMsg = "Access password is incorrect";
					return contextPath + "/chat/wordchat/roomlogin.xhtml?room=" + roomName;
				}
			}
		}

		return contextPath + "/chat/wordchat/roomlogin.xhtml?room=" + roomName;
	}

	public String deleteRoom(String contextPath) {
		FacesContext fc = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) fc.getExternalContext().getSession(true);

		String roomName = (String) session.getAttribute("roomName");
		User currUser = (User) session.getAttribute("user");
		List<ChatRoom> rooms = WordChatInitServlet.getOnlineRoomList();

		for (ChatRoom room : rooms) {
			if (roomName.equals(room.getRoomName())) {
				if (this.pwForDelete.equals(room.getPwForDelete())) {
					WordChatInitServlet.removeOnlineRoom(room);
					WMLogger.debug("User: " + currUser.getUsername() + " delete a chat room: " + roomName);
					return contextPath + "/chat/wordchat/roomlist.xhtml";
				} else {
					this.headerMsg = "Delete password is incorrect";
					return contextPath + "/chat/wordchat/roomlogin.xhtml?room=" + roomName;
				}
			}
		}

		this.headerMsg = "This chat room doesn't exist";
		return contextPath + "/chat/wordchat/roomlogin.xhtml?room=" + roomName;
	}

	public String initRoomName(String roomName) {
		FacesContext fc = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) fc.getExternalContext().getSession(true);

		if (!StringUtil.isEmpty(roomName)) {
			session.setAttribute("roomName", roomName);
		}

		return (String) session.getAttribute("roomName");
	}

	public String getHeaderMsg() {
		return headerMsg;
	}

	public void setHeaderMsg(String headerMsg) {
		this.headerMsg = headerMsg;
	}

	public String getPwForAccess() {
		return pwForAccess;
	}

	public void setPwForAccess(String pwForAccess) {
		this.pwForAccess = pwForAccess;
	}

	public String getPwForDelete() {
		return pwForDelete;
	}

	public void setPwForDelete(String pwForDelete) {
		this.pwForDelete = pwForDelete;
	}

}
